<?
include("inc_security.php");

//Khai báo biến khi hiển thị danh sách
$gs_title		= $module_name . " | Danh sách";

//Search data
$id			= getValue("id");
$keyword		= getValue("keyword", "str", "GET", "", 1);
$status		= getValue("status", "int", "GET", -1);
$sqlWhere	= "";
//Tìm theo ID
if($id > 0)			$sqlWhere .= " AND per_id = " . $id . " ";
//Tìm theo keyword
if($keyword != ""){
	if(validateDate($keyword) == 1){
		$startTime	= convertDateTime($keyword, "00:00:00");
		$endTime		= convertDateTime($keyword, "23:59:59");
		$sqlWhere	.= " AND per_date >= " . $startTime . " AND per_date <= " . $endTime . " ";
	}
	else{
		$sqlWhere	.= " AND (per_title LIKE '%" . $keyword . "%' OR per_comment LIKE '%" . $keyword . "%') ";
	}
}
if($status > -1)	$sqlWhere .= " AND per_status = " . $status . " ";

//Sort data
$sort			= getValue("sort");
switch($sort){
	case 1: $sqlOrderBy = "per_date ASC"; break;
	case 2: $sqlOrderBy = "per_date DESC"; break;
	default:$sqlOrderBy = "per_status ASC, per_date DESC"; break;
}

//Get page break params
$page_size		= 30;
$page_prefix	= "Trang: ";
$normal_class	= "page";
$selected_class= "page_current";
$previous		= "<";
$next				= ">";
$first			= "<<";
$last				= ">>";
$break_type		= 1;//"1 => << < 1 2 [3] 4 5 > >>", "2 => < 1 2 [3] 4 5 >", "3 => 1 2 [3] 4 5", "4 => < >"
$url				= getURL(0,0,1,1,"page");
$db_count		= new db_query("SELECT COUNT(*) AS count
										 FROM " . $gs_table . "
										 WHERE lang_id = " . $lang_id . $sqlWhere);
$listing_count	= mysql_fetch_array($db_count->result);
$total_record	= $listing_count["count"];
$current_page	= getValue("page", "int", "GET", 1);
if($total_record % $page_size == 0) $num_of_page = $total_record / $page_size;
else $num_of_page = (int)($total_record / $page_size) + 1;
if($current_page > $num_of_page) $current_page = $num_of_page;
if($current_page < 1) $current_page = 1;
$db_count->close();
unset($db_count);
//End get page break params
$db_listing	= new db_query("SELECT *
									 FROM " . $gs_table . "
									 WHERE lang_id = " . $lang_id . $sqlWhere . "
									 ORDER BY " . $sqlOrderBy . "
									 LIMIT " . ($current_page - 1) * $page_size . ", " . $page_size);
?>
<html>
<head>
<title><?=$gs_title?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css" media="all">@import "../css/GSportal.css";</style>
<script language="javascript" src="../js/library.js"></script>
</head>
<body>
<div class="bg_title_content">
<div class="content_title"><?=$gs_title?>: <font class="count"><?=format_number($total_record)?></font></div>
</div>
<div align="center" class="content">
<? //Page break and search data?>
<table width="98%" cellpadding="2" cellspacing="2">
	<tr>
	<?	if($total_record > $page_size){?>
		<td nowrap="nowrap"><?=generatePageBar($page_prefix, $current_page, $page_size, $total_record, $url, $normal_class, $selected_class, $previous, $next, $first, $last, $break_type)?></td>
	<? }?>
		<td align="right">
			<table cellpadding="0" cellspacing="0">
			<form name="search" action="<?=getURL(0,0,1,0)?>" method="get">
				<tr>
					<td class="form_search" nowrap="nowrap">
						ID:
						<input title="ID" type="text" class="form_control" id="id" name="id" value="<?=$id?>" maxlength="11" style="width:60px; text-align:right">&nbsp;
						Từ khóa:
						<input title="Từ khóa" type="text" class="form_control" id="keyword" name="keyword" value="<?=htmlspecialbo($keyword)?>" maxlength="255" style="width:100px">&nbsp;
						<select title="Hiển thị theo trạng thái" name="status" class="form_control">
							<option value="-1">--[Trạng thái]--</option>
						<?
						reset($arrStatus);
						foreach($arrStatus as $key => $value){
						?>
							<option value="<?=$key?>"<? if($key == $status){echo ' selected="selected"';}?>><?=$value?></option>
						<?
						}
						?>
						</select>
						<input type="hidden" name="sort" value="<?=$sort?>" />
					</td>
					<td class="form_search" style="padding-left:5px"><input title="Tìm kiếm" type="image" src="<?=$gs_imagepath?>search.gif" border="0"></td>
				</tr>
			</form>
			</table>
		</td>
	</tr>
</table>
<? //End page break and search data?>
<table class="table" border="1" bordercolor="#e5e3e6" cellpadding="3" cellspacing="0" width="98%">
	<tr class="table_title_3">
		<td>Stt.</td>
		<td>Tiêu đề</td>
		<td>Nội dung</td>
        <? foreach ($arrPollEstimate as $key => $value) {?>
        <td><?=$value?></td>
        <? }?>
		<td>
			<div>Ngày gửi</div>
			<div>
				<?=generate_sort("asc", 1, $sort, $gs_imagepath)?>
				<?=generate_sort("desc", 2, $sort, $gs_imagepath)?>
			</div>
		</td>
		<td>Xem</td>
        <td>Kích<br>hoạt</td>
		<td>Xóa</td>
	</tr>
<?
//Đếm số thứ tự
$No = ($current_page - 1) * $page_size;
while($listing = mysql_fetch_array($db_listing->result)){
	$No++;
	$bgcolor	= ($listing["per_status"] == 0) ? "#FFCCCC" : "#FFFFFF";
?>
	<tr id="tr_<?=$No?>" <?=$gs_change_bg?>>
		<td id="No_<?=$No?>" class="No" bgcolor="<?=$bgcolor?>"><?=$No?></td>
		<td><?=$listing["per_title"]?></td>
		<td><?=cut_string($listing["per_comment"], 50)?></td>
        <? foreach ($arrPollEstimate as $key => $value) {?>
        <td align="center"><span class="text_link_bold"><?=$listing['per_poll_' . $key]?></span></td>
        <? }?>
		<td align="right">
			<div><?=date("d/m/Y", $listing["per_date"])?></div>
			<div class="form_text_note"><?=date("H:i:s A", $listing["per_date"])?></div>
		</td>
		<td align="center"><img hspace="5" src="<?=$gs_imagepath?>view.gif" style="cursor:pointer" onClick="document.getElementById('No_<?=$No?>').style.background='#FFFFFF'; window.open('detail.php?record_id=<?=$listing["per_id"]?>','','height=400,width=500,menubar=0,resizable=1,scrollbars=1,statusbar=0,titlebar=0,toolbar=0')" /></td>
        <td align="center"><a href="quickset.php?type=active&record_id=<?=$listing["per_id"]?>&redirect=<?=base64_encode(getURL())?>"><img border="0" src="<?=$gs_imagepath?>active_<?=$listing["per_active"]?>.gif" /></a></td>
		<td align="center"><img title="Xóa dữ liệu" hspace="5" src="<?=$gs_imagepath?>delete.gif" style="cursor:pointer" onClick="if(confirm('Bạn có muốn xóa dữ liệu này không?')){window.location.href='delete.php?record_id=<?=$listing["per_id"]?>&redirect=<?=base64_encode(getURL())?>'}" /></td>
	</tr>
<?
}//End while($listing = mysql_fetch_array($db_listing->result))
?>
</table>
<? if($total_record > $page_size){?>
<table width="98%" cellpadding="2" cellspacing="2">
	<tr>
		<td><?=generatePageBar($page_prefix, $current_page, $page_size, $total_record, $url, $normal_class, $selected_class, $previous, $next, $first, $last, $break_type)?></td>
		<td align="right"><a title="Go to top" accesskey="T" class="top" href="#">Lên trên<img align="absmiddle" border="0" hspace="5" src="<?=$gs_imagepath?>top.gif"></a></td>
	</tr>
</table>
<? }?>
</div>
</body>
</html>
<script language="javascript">ButtonLeftFrame();</script>
<?
unset($db_listing);
?>